/**
 * 获取元素信息
 * @param {String} query 
 * @param {String} isComponent 是否为组件 
 */
export function getDomInfo(query,isComponent = false) {
	return new Promise((resolve,reject) => {
		if(isComponent) {
			uni.createSelectorQuery().in(this).select(query).boundingClientRect(e => {
				resolve(e)
			}).exec()
			return
		}
		uni.createSelectorQuery().select(query).boundingClientRect(e => {
			resolve(e)
		}).exec()
	})
}

/**
 * 根据元素跳转页面指定位置
 * @param {String} selector 
 * @param {Number} duration 
 */
export function scrollToSelector(selector = '', duration = 0) {
	uni.pageScrollTo({
		duration: duration,
		selector: selector
	})
}

/**
 * 根据scroll值跳转页面指定位置
 * @param {String} selector 
 * @param {Number} duration 
 */
export function scrollTo(scrollTop = 0, duration = 0) {
	uni.pageScrollTo({
		duration: duration,
		scrollTop: scrollTop
	})
}
/**
 * 获取url地址参数
 * @param {String} url 
 */
export function getUrlParams(url) {
	if(url.indexOf('?') != -1){
		let obj = {};
		let arr = url.slice(url.indexOf('?')+1).split('&');
		arr.forEach(item => {
			let param = item.split('=');
			obj[param[0]] = param[1];
		})
		return obj
	}else {
		return {}
	}
}
/**
 * 获取当前日期 yyyy-MM-dd HH:mm:ss  星期W
 * @param {String} formatStr
 */
export function getDate(formatStr) {
		let str = formatStr;   
	    const Week = ['日','一','二','三','四','五','六'];  
		const date = new Date()
	    str = str.replace(/yyyy|YYYY/,date.getFullYear());   
	    str = str.replace(/yy|YY/,(date.getYear() % 100)>9?(date.getYear() % 100).toString():'0' + (date.getYear() % 100));   
	  
	    str = str.replace(/MM/,date.getMonth()>9?date.getMonth().toString():'0' + (date.getMonth()+1));      
	    str = str.replace(/M/g,date.getMonth());   
	 
	 
	    str = str.replace(/w|W/g,Week[date.getDay()]);   
	  
	    str = str.replace(/dd|DD/,date.getDate()>9?date.getDate().toString():'0' + date.getDate());   
	    str = str.replace(/d|D/g,date.getDate());   
	  
	    str = str.replace(/hh|HH/,date.getHours()>9?date.getHours().toString():'0' + date.getHours());   
	    str = str.replace(/h|H/g,date.getHours());   
	    str = str.replace(/mm/,date.getMinutes()>9?date.getMinutes().toString():'0' + date.getMinutes());   
	    str = str.replace(/m/g,date.getMinutes());   
	  
	    str = str.replace(/ss|SS/,date.getSeconds()>9?date.getSeconds().toString():'0' + date.getSeconds());   
	    str = str.replace(/s|S/g,date.getSeconds());   
		
	    return str;  
}
/**计算日期差 
 * @param {String} date1
 * @param {String} date2
 */
export function daysDiff(date1, date2) {
	let dateTime1 = new Date(date1)
	let dateTime2 = new Date(date2)
	return Math.ceil((dateTime1 - dateTime2) / 86400000);
}

// 随机数函数
function getRandomNum(min, max) {
  // 返回[min, max]之间的随机数
  return Math.floor(Math.random() * (max - min + 1) + min)
}
// 洗牌函数
export function shuffle(array) {
  let newArr = array.slice()  // 不改变原数组，将数组剪切一份给newArr
  for (let i = 0; i < newArr.length; i++) {
	const j = getRandomNum(0, i)
	const temp = newArr[i]
	newArr[i] = newArr[j]
	newArr[j] = temp
  }
  return newArr
}